import { Component, OnInit, ViewChild, Output, Input, EventEmitter } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { LocalStorage } from '@core/local.storage';
import { _HttpClient } from '@delon/theme';
import { DatePipe } from '@angular/common';
import { environment } from '../../../../environments/environment';
import { TableCheckPageComponent } from '../../../layout/components/tableCheckPage.component';

@Component({
    selector: 'app-detailmodal',
    templateUrl: './detailmodal.component.html',
    styleUrls: ['./problemStatistics.component.less']
})
export class DetailModalComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableCheckPageComponent;
    @Input() apiUrl: any;
    @Input() width: any;
    @Input() responseData: any;
    @Input() popTitle: any;
    @Input() sTime: any;
    @Input() eTime: any;
    @Input() paramType: any
    @Output() public closeModal = new EventEmitter();
    @Output() public reloadData = new EventEmitter();

    Visible = false;
    operaData: any = {};
    objData: any = {};
    thisData: any = { 'count': 0, 'id': [] };
    problemData: any = {};
    processesData = [];
    handlesData = [];
    headData = [];
    plainData = [];
    tableScroll = { x: '2000px' };

    constructor(
        private msg: NzMessageService,
        private sanitizer: DomSanitizer,
        public ls: LocalStorage,
        private http: _HttpClient,
        private datePipe: DatePipe
    ) { }

    ngOnInit() {
        if (!this.width) {
            this.width = '500';
        }
        setTimeout(() => {
            this.Visible = true;
            this.headData = [
                { "name": "状态", "width": "105px" },
                { "name": "工作项目", "width": "8%" },
                { "name": "检查部门", "width": "6%" },
                { "name": "检查时间", "width": "5%" },
                { "name": "检查地点", "width": "4%" },

                { "name": "检查人", "width": "3.5%" },
                { "name": "责任部门", "width": "6%" },
                { "name": "问题描述", "width": "10%" },
                { "name": "整改要求" },
                { "name": "整改期限", "width": "5%" },

                { "name": "销号部门", "width": "6%" },
                { "name": "问题类别", "width": "5%" },
                { "name": "设备类型", "width": "6%" },
                { "name": "备注", "width": "5%" }
            ];
            this.plainData = [
                "cancledStatus", "projectName", "checkOrgName", "checkDate", "checkPlace",
                "checkPerson", "chargeOrgName", "problemDesc", "handleRequire", "deadline",
                "cancleOrgName", "typeName", "devTypeName", "remark"
            ];
            this.loadList();
        }, 200);
    }
    loadList() {
        const data = JSON.parse(this.responseData.split('|')[0]);
        let param = '&deadlineL=' + this.datePipe.transform(this.sTime, 'yyyy-MM-dd') + '&deadlineR=' + this.datePipe.transform(this.eTime, 'yyyy-MM-dd');
        switch (this.paramType) {
            case 'class':
                param += '&typeName=' + data.typeName;
                break;
            case 'org':
                switch (this.responseData.split('|')[1]) {
                    case 'nonOver':
                    case 'over':
                    case 'total':
                    case 'ratio':
                        param += '&orgId=' + data.orgId;
                        break;
                    default:
                        param += '&orgId=' + data.orgId;
                        param += '&typeName=' + this.responseData.split('|')[1];
                        break;
                }
                break;
            case 'project':
                switch (this.responseData.split('|')[1]) {
                    case 'nonOver':
                    case 'over':
                    case 'total':
                    case 'ratio':
                        param += '&projectId=' + data.projectId;
                        break;
                    default:
                        param += '&projectId=' + data.projectId;
                        param += '&orgName=' + this.responseData.split('|')[1];
                        break;
                }
                break;
        }
        if (this.responseData.split('|')[1] === 'over') {
            param += '&over=1';
        }
        if (this.responseData.split('|')[1] === 'nonOver') {
            param += '&over=0';
        } 
        this.tcp.loadList(environment.project + '/safeProblem/groupDetail', param);
    }
    Ok() {
        this.Cancel();
    }
    Cancel() {
        this.Visible = false;
        setTimeout(() => {
            this.closeModal.emit();
        }, 200);
    }
}